<?php
class Form_Data
{
    public static function remove ($form_type, $id)
    {
        $info = DB::select('table', 'primary_key')
            ->from('form_config')
            ->where('name', '=', $form_type)
            ->limit(1)
            ->execute()
            ->current();

        if (empty($info))
            return;

        $table = Arr::get($info, 'table');
        $pkey  = Arr::get($info, 'primary_key');

        DB::delete($table)
            ->where($pkey, '=', $id)
            ->limit(1)
            ->execute();
    }

    public static function create ($form_type, $data)
    {
        $query = DB::select('input_name', 'options')
            ->from('form_items_config')
            ->where('type', '=', $form_type)
            ->execute();

        $keys = array();
        $opts = array();
        foreach ($query as $rs)
        {
            $keys[] = $rs['input_name'];
            $opts[$rs['input_name']] = $rs['options'];
        }

        $table = DB::select('table')
            ->from('form_config')
            ->where('name', '=', $form_type)
            ->limit(1)
            ->execute()
            ->get('table');

        $row = array();
        foreach ($keys as $key)
        {
            if (isset($data[$key]))
                $row[$key] = $data[$key];
        }
        if (in_array('created', $keys))
        {
            if ($opts['created'] == 'format="DATE"')
                $row['created'] = DB::expr('curdate()');
            else
                $row['created'] = DB::expr('unix_timestamp(now())');
        }

        if (in_array('changed', $keys))
        {
            if ($opts['changed'] == 'format="DATE"')
                $row['changed'] = DB::expr('curdate()');
            else
                $row['changed'] = DB::expr('unix_timestamp(now())');
        }

        self::ignoreUnExistsColumns ($table, $row);

        list($insert_id, $rows) = DB::insert($table)
            ->columns (array_keys($row))
            ->values (array_values($row))
            ->execute();

        return $insert_id;
    }

 

    public static function update ($form_type, $nid, $data)
    {
        $query = DB::select('input_name', 'options')
            ->from('form_items_config')
            ->where('type', '=', $form_type)
            ->execute();

        $keys = array();
        $opts = array();
        foreach ($query as $rs)
        {
            $keys[] = $rs['input_name'];
            $opts[$rs['input_name']] = $rs['options'];
        }

        $tinfo = DB::select('table', 'primary_key')
            ->from('form_config')
            ->where('name', '=', $form_type)
            ->limit(1)
            ->execute()
            ->current();

        $table = $tinfo['table'];
        $pkey  = $tinfo['primary_key'];

        $row = array();
        foreach ($keys as $key)
        {
            if (isset($data[$key]))
                $row[$key] = $data[$key];
        }
        if(isset($row['id']))
        unset($row['id']);
        if(isset($row['created']))
        unset($row['created']);

        if (in_array('changed', $keys))
        {
            if ($opts['changed'] == 'format="DATE"')
                $row['changed'] = DB::expr('curdate()');
            else
                $row['changed'] = DB::expr('unix_timestamp(now())');
        }

        self::ignoreUnExistsColumns ($table, $row);

        $rows = DB::update($table)
            ->set($row)
            ->where($pkey, '=', $nid)
            ->execute();
    }

    public static function detail ($form_type, $nid)
    {
        $query = DB::select('table', 'primary_key')
            ->from('form_config')
            ->where('name', '=', $form_type)
            ->limit(1)
            ->execute()
            ->current();

        $table = $query['table'];
        $pkey  = $query['primary_key'];

        $query = DB::select()
            ->from($table)
            ->where($pkey, '=', $nid)
            ->limit(1)
            ->execute();

        return $query->current();
    }

    public static function set_sorted($form_type, $sorts, $extra = NULL)
    {
        $table = DB::select('table')
            ->from('form_config')
            ->where('name', '=', $form_type)
            ->limit(1)
            ->execute()
            ->get('table');


        foreach ($sorts as $id => $val)
        {
            $query = DB::update($table)
                ->set(array('sort' => $val));

            if (is_array($extra) && ! empty($extra))
            {
                foreach ($extra as $arr)
                {
                    $query->set(array($arr[0] => $arr[1]));
                }
            }

            $query->where('id', '=', $id)
                ->limit(1)
                ->execute();
        }
    }

    private static function ignoreUnExistsColumns ($table, & $row)
    {
        $columns = Database::instance()->list_columns($table);
        $keyNames = array();
        $keyRowNames = array();

        foreach ($row as $key => $rs)
        {
            $k = strtoupper($key);
            $keyRowNames[$k] = $key;
        }

        foreach ($columns as $col)
        {
            $k = strtoupper($col['column_name']);
            $keyNames[] = $k;
        }


        foreach ($keyRowNames as $upkey => $key)
        {
            if (! in_array($upkey, $keyNames))
            {
                unset($row[$key]);
            }
        }
    }
}
